﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.BUS.Workers;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmOutFormInfo : frmInfo
    {
        public frmOutFormInfo()
        {
            InitializeComponent();

            this.Load += frmOutFormInfo_Load;
        }

        void frmOutFormInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            this.ucOutInfo1.CurrentOutInfo = this.entity as PlanOutInfo;
            this.ucOutInfo1.LoadInformation();

            this.ddlUser.DataSource = UserInfoHelper.GetUsers();
            this.ddlUser.DisplayMember = "UserName";
            this.ddlUser.ValueMember = "UserId";


            var worker = new ComputeOutFrmHelper();
            worker.id = (this.entity as PlanOutInfo).plan_out_id;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();
        }


        private ComputeResult result = null;
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            frmloading.Close();


         //   resource_Id


            result = e.Result as ComputeResult;
            resource_Id.DataSource = ResourcesHelper.GetAllResources();
            resource_Id.DisplayMember = "ResourceName";
            resource_Id.ValueMember = "ResourceId";

            this.grd.DataSource = (e.Result as ComputeResult).result_shown;


            if (result.IsShowReCheckButton)
            {
                this.btnCheck.Visible = true;}

            foreach (var msg in (e.Result as ComputeResult).messages)
            {
                if (string.IsNullOrEmpty(this.lblMessages.Text))
                    this.lblMessages.Text = msg;
                else{
                    this.lblMessages.Text = this.lblMessages.Text+"\t\n"+msg;
                }
            }

            //show the 重新检验的按钮。
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (result == null)
                return;

            if (result.messages.Count > 0)
            {
                MessageBox.Show("存在异常，不能出库.","信息提示");
                return;
            }

            if (MessageBox.Show("确定出库?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
                return;

            var currentout = this.entity as PlanOutInfo;
            currentout.plan_out_get_by = (int)this.ddlUser.SelectedValue;
            currentout.plan_out_get_time = this.txtGetdate.Value;

            var dt = result.result;
            var detailIds = new List<int>();
            foreach (DataRow dr in dt.Rows)
            {
                var detail_id = int.Parse(dr["supply_detail_Id"].ToString());
                detailIds.Add(detail_id);
            }

          var createresult=  PlanHelper.CreateStorageOutData(currentout,detailIds);

            if (createresult == true)
            {
                currentout.plan_isout = true;
                this.Close();
            }
        }

        private void ucOutInfo1_Load(object sender, EventArgs e)
        {

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("这将把过期的物料重新质检?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                DialogResult.No)
                return;

            
            //将过期的物料设置为expired.
            var worker = new CommonBackgroundWorker();
            worker.m_delegateFunction = new DelegateFunction(
                () => UpdateExceptionData());
            worker.RunWorkerCompleted +=setworker_RunWorkerCompleted;
            worker.RunWorkerAsync();

            frmloading.ShowDialog();

        }

        private void setworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
         //   throw new NotImplementedException();
            //
            frmloading.Close();
            if (MessageBox.Show("重新加载出库数据?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                DialogResult.No)
                return;
            //重新load 
            var worker = new ComputeOutFrmHelper();
            worker.id = (this.entity as PlanOutInfo).plan_out_id;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync();
            frmloading = new frmLoading();
            frmloading.ShowDialog();


        }


        private object UpdateExceptionData()
        {
            foreach (DataRow data in result.result_shown.Rows)
            {
                if (data["supply_invalidate"] == null)
                {
                    continue;
                }

                var invalidate = DateTime.Parse(data["supply_invalidate"].ToString());
                if (invalidate < DateTime.Now)
                {
                    int supplyID = int.Parse(data["supply_id"].ToString());
                    SupplyInfoHelper.SetExpireStorageStatus(supplyID);
                }
            }
            return null;
        }
      


    }
}
